﻿namespace com.crownpack.Extensions.SharePointExtensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Microsoft.SharePoint;

    public static class SPWebCollectionExtensions
    {
        #region Methods

        public static bool ContainsSubweb( this SPWebCollection webs, string webUrl )
        {
            using ( SPWeb childWeb = webs.FirstOrDefault( a => a.Name.Equals( webUrl ) ) )
            {
                return childWeb == null ? false : true;
            }
        }

        public static IEnumerable<TResult> Select<TResult>( this SPWebCollection webs, Func<SPWeb, TResult> selector )
        {
            foreach ( SPWeb web in webs )
            {
                TResult ret = selector( web );
                web.Dispose( );
                yield return ret;
            }
        }

        #endregion Methods
    }
}